package com.ahyesyesteam.aicarfaultrepair.config;

import com.ahyesyesteam.aicarfaultrepair.handler.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

//解决跨域问题
@Configuration
public class CorsConfiguration implements WebMvcConfigurer {

    @Autowired
    LoginInterceptor loginInterceptor;

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                //允许跨域的域名
                .allowedOriginPatterns("*")
                //是否允许证书
                .allowCredentials(true)
                //允许跨域的方法
                .allowedMethods("GET", "POST", "DELETE", "PUT","OPTIONS")
                //maxAge(3600)表明在3600秒内，不需要再发送预检验请求，可以缓存该结果
                .maxAge(3600);
    }

    // 拦截用户请求,判断是否登陆,如果没有登陆, 将会阻止访问
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginInterceptor)
                .addPathPatterns("/comments/**")
                .excludePathPatterns("/comments/list/{solution_id}")
                .addPathPatterns("/solutions/like/{solution_id}");

    }
}
